<?php

namespace addons\qingdong\model;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

/**
 *审批记录
 */
class ExamineRecord Extends Model {
	use SoftDelete;
    // 表名,不含前缀
    protected $name = 'qingdong_examine_record';

	const   CONSUME_TYPE     = 'consume';//费用
	const   CONTRACT_TYPE    = 'contract';//合同
	const   RECEIVABLES_TYPE = 'receivables';//回款
	const   ACHIEVEMENT_TYPE = 'achievement';//业绩目标
    const   APPROVAL_TYPE = 'approval';//审批
    const   CARD_TYPE = 'card';//补卡
    const   LEAVE_TYPE = 'leave';//请假
	// 开启自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $deleteTime = 'deletetime';


	public function checkStaff() {
		return $this->hasOne(Staff::class, 'id', 'check_staff_id')->field('id,name,img');
	}


	//获取审批记录
	public static function getList($relation_type, $relation_id) {
		return self::where([
			'relation_type' => $relation_type,
			'relation_id'   => $relation_id
		])->with(['checkStaff'])->field('content,check_staff_id,check_time,status,createtime')->order('id asc')->select();
	}


	public function getCheckTimeAttr($value) {
		if ($value) {
			return date('Y-m-d H:i', $value);
		}

		return $value;
	}

	public function getCreatetimeAttr($value) {
		if ($value) {
			return date('Y-m-d H:i', $value);
		}

		return $value;
	}

	/**
	 * 添加审批
	 * @param $relation_type string 关联类型
	 * @param $relation_id int 关联id
	 * @param $staff_id int 审批人ID
	 */
	public static function addExaminse($relation_type, $relation_id, $staff_id) {
		$staff = Staff::info();
		$data   = [
			'relation_type'  => $relation_type,
			'relation_id'    => $relation_id,
			'check_staff_id' => $staff_id,
			'status'         => 0
		];
        if (self::where($data)->find()) {//已发送过审批通知
            return true;
        }
		$Model  = new self;
		$result = $Model->save($data);
		if (false === $result) {
			// 验证失败 输出错误信息
			throw new Exception($Model->getError());
		}
		//发送通知
		Message::addMessage(Message::EXAMINE_TYPE, $Model->getLastInsID(), $staff_id, $staff->id);

		return true;
	}


	/**
	 *是否审批
	 */
	public static function isExaminse($relation_type, $relation_id) {
		$staff = Staff::info();
		if (self::where([
			'relation_type'  => $relation_type,
			'relation_id'    => $relation_id,
			'status'         => 0,
			'check_staff_id' => $staff->id
		])->find()) {
			return 1;
		}

		return 0;
	}
    //合同
    public function contract() {
        return $this->hasOne(Contract::class, 'id', 'relation_id')
            ->field('id,name,owner_staff_id');
    }
    //费用
    public function consume() {
        return $this->hasOne(Consume::class, 'id', 'relation_id')->field('id,consume_time,consume_type,money,staff_id');
    }
    //回款
    public function receivables() {
        return $this->hasOne(Receivables::class, 'id', 'relation_id')->field('id,return_time,return_type,number,create_staff_id,money');
    }
    //业绩目标
    public function achievement() {
        return $this->hasOne(AchievementRecords::class, 'id', 'relation_id');
    }
    //办公审批
    public function approval() {
        return $this->hasOne(Approval::class, 'id', 'relation_id')->field('id,formapproval_id,create_staff_id');
    }
    public static function cancelExaminse($relation_type, $relation_id)
    {
        $record = ExamineRecord::where([
            'relation_type' => $relation_type,
            'relation_id'   => $relation_id,
            'status'        => 0
        ])->find();
        if(empty($record)){
            return true;
        }
        if ($message = Message::where([
            'relation_type' => 'examine',
            'relation_id'   => $record['id'],
        ])->find()) {
            Message::where(['id' => $message['id']])->update(['status' => 1, 'read_time' => time()]);
        }
        ExamineRecord::where([
            'relation_type' => $relation_type,
            'relation_id'   => $relation_id,
            'status'        => 0
        ])->update(['status' => 3]);
        return true;
    }
}
